﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.AdverseEvent
{
    public class AdverseEventReasonController : BaseController
    {
        // GET: AdverseEvent/AdverseEventReason
        public ActionResult Index(int typeID = 0, string typeName = "")
        {
            ViewBag.TypeID = typeID;
            ViewBag.TypeName = typeName;
            return View();
        }

        /// <summary>
        /// 根因列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            SelectBuilder data = new SelectBuilder();
            data.From = "AdverseEventReason";
            data.Select = "ID,ParentID,Reason,Sort";
            data.OrderBy = "Sort ASC";
            data.WhereSql = GetWhereSql();
            DataTable dt = BaseService<DataTable>.GetDataTableSelectBuilder(data);
            var result = new { Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string GetWhereSql()
        {
            int typeID = DataConvert.StrToInt(Request["typeID"]);
            string whereSql = "1 = 1";
            if (typeID > 0)
            {
                whereSql += string.Format(" and TypeID = {0}", typeID);
            }
            else
            {
                whereSql = "1 != 1";
            }
            return whereSql;
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            AdverseEventReason adverseEventReason = new AdverseEventReason();
            if (id > 0)
            {
                adverseEventReason = AdverseEventReasonService.GetQuerySingleByID(id);
            }
            ViewBag.AdverseEventReason = adverseEventReason;
            return View();
        }

        /// <summary>
        /// 添加/编辑根因
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(AdverseEventReason adverseEventReason)
        {
            OperateResult resultInfo = AdverseEventReasonManager.Save(adverseEventReason);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取上级根因列表
        /// </summary>
        /// <param name="typeID"></param>
        /// <returns></returns>
        public ActionResult GetParentReasonList(int typeID)
        {
            DataTable dt = AdverseEventReasonService.GetAdverseEventReasonTable(typeID);
            if (dt.Rows.Count > 0)
            {
                var d = (from r in dt.AsEnumerable() where r.Field<int>("Level") <= 2 select r);
                dt = d.CopyToDataTable<DataRow>();
            }
            return JsonDate(dt);
        }

        /// <summary>
        /// 删除根因
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = AdverseEventReasonManager.Delte(id);
            return JsonDate(resultInfo);
        }
    }
}